//
//  BKLHealth_MCDiseaseController.m
//  BKLMedicalCare
//
//  Created by ma c on 2017/6/14.
//  Copyright © 2017年 q mac. All rights reserved.
//

#import "BKLHealth_MCDiseaseController.h"
#import "BKLHealth_MCView.h"
#import "BKLHealth_MCCommon.h"
#import "BKLHealth_MCCollectionCell.h"
#import "BKLHealth_MCAppointmentOfMedicalController.h"

#pragma mark - title cell
#pragma mark -

@implementation BKLHealth_MCPATitleCell
{
    UILabel *_titleLab;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    _titleLab = [[UILabel alloc] init];
    _titleLab.textColor = kBklHealth_bklLightGray;
    _titleLab.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_titleLab];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)didSelected {
    _titleLab.textColor = kBklHealth_bklYellow;
}

- (void)didDeselected {
    _titleLab.textColor = kBklHealth_bklLightGray;
}

- (void)fillData:(id)data {
    if (!data) {
        return;
    }
    _titleLab.text = data[@"title"];
}

+ (NSValue *)computeSize:(id)data {
    NSString *title = data[@"title"];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]}];
    return [NSValue valueWithCGSize:size];
}
@end

#pragma mark - 视图控制器
#pragma mark -

@interface BKLHealth_MCDiseaseController ()
<
    BKLHealth_MCAppointmentOfMedicalViewDelegate,
    BKLHealth_MCMultipleTitlesViewDelegate,
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout,
    UIScrollViewDelegate,
    BKLHealth_MCCArticlePageCellDelegate
>
@property (nonatomic, strong)BKLHealth_MCAppointmentOfMedicalView *appiontmentView;
@property (nonatomic, strong)UIView *layoutView;
@property (nonatomic, strong)UILabel *titleLab;
@property (nonatomic, strong)BKLHealth_MCMultipleTitlesView *titlesView;
@property (nonatomic, strong)UICollectionView *articleCollection;
@property (nonatomic, strong)NSArray *dataSource;
@property (nonatomic, assign)BOOL isFirstShowInWindow;
@end

@implementation BKLHealth_MCDiseaseController
static NSString *const bklHealth_mccArticleCellIdentifier = @"bklHealth_mccArticleCellIdentifier";


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (_isFirstShowInWindow) {
        [_titlesView selectedItemAtIndex:0 animated:NO];
        _isFirstShowInWindow = NO;
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _isFirstShowInWindow = YES;
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self initializeComponents];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initializeComponents {
    
    _dataSource = [NSArray array];

    // 预约体检视图
    _appiontmentView = [[BKLHealth_MCAppointmentOfMedicalView alloc] init];
    _appiontmentView.deleagte = self;
    [self.view addSubview:_appiontmentView];
    
    // 布局使用视图
    _layoutView = [[UIView alloc] init];
    _layoutView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_layoutView];
    
    // 热门文章 label
    _titleLab = [[UILabel alloc] init];
    _titleLab.textColor = kBklHealth_bklBlack;
    _titleLab.font = [UIFont systemFontOfSize:14];
    _titleLab.text = @"热门文章";
    [self.view addSubview:_titleLab];
    
    // 多标题视图
    _titlesView = [[BKLHealth_MCMultipleTitlesView alloc] init];
    _titlesView.collectionCellClassName = NSStringFromClass([BKLHealth_MCPATitleCell class]);
    _titlesView.titleSource = @[
                                @{@"title" : @"精选"},
                                @{@"title" : @"感冒"},
                                @{@"title" : @"发烧"},
                                @{@"title" : @"头痛"},
                                @{@"title" : @"腹泻"},
                                @{@"title" : @"吃的有点多"},
                                @{@"title" : @"晚上吃啥"}
                                ];
    _titlesView.leftSpacing = 12;
    _titlesView.rightSpacing = 12;
    _titlesView.lineSpacing = 54;
    _titlesView.deleagte = self;
    [self.view addSubview:_titlesView];
    
    
    _dataSource = [self creatData];
    _articleCollection = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        collection.backgroundColor = [UIColor clearColor];
        collection.showsHorizontalScrollIndicator = NO;
        collection.pagingEnabled = YES;
        collection.delegate = self;
        collection.dataSource = self;
        [collection registerClass:[BKLHealth_MCCArticlePageCell class] forCellWithReuseIdentifier:bklHealth_mccArticleCellIdentifier];
        collection;
    });
    [self.view addSubview:_articleCollection];
    
    [self makeConstraints];
}

- (void) makeConstraints {
    [_appiontmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(1);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(72);
    }];
    
    [_layoutView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_appiontmentView.mas_bottom).offset(8);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_layoutView).offset(18);
        make.left.mas_equalTo(_layoutView).offset(12);
    }];
    
    [_titlesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(36);
    }];
    
    [_articleCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titlesView.mas_bottom);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}

- (NSArray *)creatData {
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 7; i++) {
        NSMutableArray *subArr = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            [subArr addObject:@{@"imageUrl" : @"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1066462716,4040550561&fm=26&gp=0.jpg",
                                @"title" : @"不知道不知道不知道不知道不知道不知道",
                                @"deputyText" : @"中央电视台"
                                }];
        }
        [arr addObject:subArr];
    }
    return arr;
}

- (NSInteger)currentArticleIndex {
    NSInteger index = (_articleCollection.contentOffset.x + _articleCollection.frame.size.width * 0.5) / _articleCollection.frame.size.width;
    return index;
}

#pragma mark - 预约体检视图 delegate
- (void)appointmentOfMedicalViewDidSelected:(BKLHealth_MCAppointmentOfMedicalView *)view {
    NSLog(@"预约体检");
    BKLHealth_MCAppointmentOfMedicalController *aomvc = [[BKLHealth_MCAppointmentOfMedicalController alloc] init];
    [self.navigationController pushViewController:aomvc animated:YES];
}

#pragma mark - 热门文章多标题视图 delegate
- (void)multipleTitlesView:(BKLHealth_MCMultipleTitlesView *)view didSelectedItemAtIndexPath:(NSIndexPath *)indexPath {
    [_articleCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
#pragma mark - 热门文章 collection cell delegate
- (void)articlePageView:(BKLHealth_MCCArticlePageCell *)cell didSelectedItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSIndexPath *collectionIndexPath = [_articleCollection indexPathForCell:cell];
    NSIndexPath *dataIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:collectionIndexPath.row];
    NSLog(@"热门文章点击 index path : %@", dataIndexPath);
}

#pragma mark - 热门文章 collection view delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BKLHealth_MCCArticlePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:bklHealth_mccArticleCellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    BKLHealth_MCCArticlePageCell *bklCell = (BKLHealth_MCCArticlePageCell *)cell;
    [bklCell fillData:_dataSource[indexPath.row]];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return collectionView.frame.size;
}

#pragma mark - scroll view delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentIndex = [self currentArticleIndex];
    [_titlesView selectedItemAtIndex:currentIndex animated:YES];
}
@end
